import 'package:faker/i18n/i18n.dart';
import 'package:faker/res/style.dart';
import 'package:faker/service/rssapi.dart';
import 'package:faker/utils/toast_util.dart';
import 'package:faker/widgets/base_widget.dart';
import 'package:faker/widgets/loading_dialog.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

/// 新增或编辑TODO
class ConfigServerScreen extends BaseWidget {
  final String server;

  ConfigServerScreen({this.server});

  @override
  State<StatefulWidget> createState() {
    return _ConfigServerScreenSate();
  }

  @override
  BaseWidgetState<BaseWidget> attachState() {
    // TODO: implement attachState
    return null;
  }
}

class _ConfigServerScreenSate extends BaseWidgetState<ConfigServerScreen> {
  TextEditingController _serverController = TextEditingController();

  final FocusNode _serverFocusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    setAppBarVisible(false);
    // 判断 bean 是否有值
    if (widget.server != null) {
      _serverController.text = widget.server;
    }
  }

  /// 构造分割线
  Widget buildDivider() {
    return new Divider(height: 1);
  }

  /// 显示Loading
  _showLoading(BuildContext context) {
    showDialog(
        context: context,
        barrierDismissible: false,
        builder: (_) {
          return new LoadingDialog(
            outsideDismiss: false,
            loadingText: S.of(context).saving,
          );
        });
  }

  /// 隐藏Loading
  _dismissLoading(BuildContext context) {
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 触摸收起键盘
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          body: Container(
            padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.fromLTRB(0, 120, 0, 16),
                  child: Row(
                    children: <Widget>[
                      Text(
                        S.of(context).youServer + "(e.g.:http://yousite.com)",
                        style: TextStyle(fontSize: 16),
                      )
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.fromLTRB(0, 16, 0, 16),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: TextField(
                          focusNode: _serverFocusNode,
                          autofocus: false,
                          enabled: true,
                          controller: _serverController,
                          style: TextStyle(
                            fontFamily: fontName,
                            fontSize: 16,
                            color: Theme.of(context).textSelectionColor,
                          ),
                          cursorColor: Colors.blue,
                          decoration: InputDecoration.collapsed(
                            hintText: _serverController.text,
                          ),
                          maxLines: 1,
                        ),
                      )
                    ],
                  ),
                ),
                buildDivider(),
                Padding(
                  padding: const EdgeInsets.only(bottom: 20),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: RaisedButton(
                          padding: EdgeInsets.all(16.0),
                          elevation: 0.5,
                          child: Text("保存"),
                          color: Theme.of(context).primaryColor,
                          textColor: Colors.white,
                          onPressed: () async {
                            ///Save
                            final prefs = await SharedPreferences.getInstance();
                            prefs.setString('server', _serverController.text);
                            RssApi().setServer(_serverController.text);
                            T.show(msg: S.of(context).updateSucceed);
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ));
  }

  @override
  AppBar attachAppBar() {
    // TODO: implement attachAppBar
    return null;
  }

  @override
  Widget attachContentWidget(BuildContext context) {
    // TODO: implement attachContentWidget
    return null;
  }

  @override
  void onClickErrorWidget() {
    // TODO: implement onClickErrorWidget
  }
}
